package gulimall;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gulimall.constant.CartConstant;
import gulimall.pojo.vo.CartItemVO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author HLF
 * @date 2022/8/7 16:04
 */
@SpringBootTest
public class MyTests01 {
    @Test
    void test01() {
        BigDecimal price = new BigDecimal(20);
        System.out.println(price.multiply(BigDecimal.valueOf(4)));
    }


    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    void test02() {
        BoundHashOperations<String, Object, Object> hash1 = redisTemplate.boundHashOps("hash1");
        hash1.put("k1", "v1");
    }

    @Test
    void test03() throws JsonProcessingException {
        //1.获取当前在线用户的购物车
        BoundHashOperations<String, String, Object> hashOps = redisTemplate.boundHashOps(CartConstant.CACHE_PREFIX + "6");

        Object o = hashOps.get(String.valueOf(49));
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(o);

        CartItemVO cartItemVO = mapper.readValue(s, CartItemVO.class);

        System.out.println(cartItemVO);
    }

    @Test
    void test04() throws Exception{
        BoundHashOperations<String, String, CartItemVO> hashOps = redisTemplate.boundHashOps(CartConstant.CACHE_PREFIX + "6");

        CartItemVO cartItemVO = hashOps.get("42");

        System.out.println(cartItemVO);
    }
}
